// goodsInfo.js
const url = getApp().globalData.API_URL;
const Uid = getApp().globalData.uid;
const app = getApp();
Component({
  data: {
    inv: 0,
    uid: app.globalData.uid,
    pid: 0,
    value: '0', //数量计数
    caloricSum: '',
    goodsInfo: {},
    kindtext: ['饮食', '运动'],
    upuid: app.globalData.uid,
    upName: '餐饮',
    upInfo: '',
    upSpecification: 'icon-canyin',
    upCaloric: '',
    upKind: 1,
    //时间选择器
    mode: '',
    dateVisible: false,
    year: new Date().getFullYear(), 
    month: new Date().getMonth()+1, 
    day: new Date().getDate(), 
    dateText: '',
    timeStamp: '', //传给后端的时间戳，年月日为选中的日期，时分秒为当前时间
    currentCategorySelect: 'icon-canyin',
		currentCategoryColor: '#7392ff',
		currentCategoryName: '餐饮',
		currentCategorySelect1: 'icon-canyin',
		currentCategoryColor1: '#7392ff',
		currentCategoryName1: '餐饮',
		currentCategorySelect2: 'icon-gongzi',
		currentCategoryColor2: '#fd8008',
    currentCategoryName2: '工资',
    categoryList: [
    {
      "name": "餐饮",
      "iconStyleName": "icon-canyin",
      "color": "#02c39a"
    },
    {
      "name": "交通",
      "iconStyleName": "icon-jiaotong",
      "color": "#02c39a"
    },
    {
      "name": "住房",
      "iconStyleName": "icon-zhufang",
      "color": "#02c39a"
    },
    {
      "name": "家庭",
      "iconStyleName": "icon-080jiating",
      "color": "#02c39a"
    },
    {
      "name": "宠物",
      "iconStyleName": "icon-chongwu",
      "color": "#02c39a"
    },
    {
      "name": "购物",
      "iconStyleName": "icon-gouwu",
      "color": "#02c39a"
    },
    {
      "name": "充值",
      "iconStyleName": "icon-chongzhi",
      "color": "#02c39a"
    },
    {
      "name": "水电",
      "iconStyleName": "icon-shuidian",
      "color": "#02c39a"
    },
    {
      "name": "医疗",
      "iconStyleName": "icon-yiliao",
      "color": "#02c39a"
    },
    {
      "name": "烟酒",
      "iconStyleName": "icon-yanjiu",
      "color": "#02c39a"
    },
    {
      "name": "旅游",
      "iconStyleName": "icon-lvyou",
      "color": "#02c39a"
    },
    {
      "name": "借出",
      "iconStyleName": "icon-jiechu",
      "color": "#02c39a"
    },
    {
      "name": "其他",
      "iconStyleName": "icon-qita",
      "color": "#02c39a"
    },
  ],
  categoryList2: [{
    "name": "工资",
    "iconStyleName": "icon-gongzi",
    "color": "#02c39a"
  },
  {
    "name": "兼职",
    "iconStyleName": "icon-jianzhi",
    "color": "#02c39a"
  },
  {
    "name": "理财",
    "iconStyleName": "icon-licai",
    "color": "#02c39a"
  },
  {
    "name": "礼金",
    "iconStyleName": "icon-lijin",
    "color": "#02c39a"
  },
  {
    "name": "其它",
    "iconStyleName": "icon-qita",
    "color": "#02c39a"
  },
  ],
    // upKind: null,
  },
  methods: {
    
    tabclick: function(e) {
      let inv = e.currentTarget.dataset.inv; // 获取点击事件携带的数据
      if (inv === '0') {
          this.setData({
              inv: inv,
              upName:'餐饮',
              upSpecification: 'icon-canyin',
          });
      } else if (inv === '1') {
          this.setData({
              inv: inv,
              upName:'工资',
              upSpecification: 'icon-gongzi',
          });
      }
    },
    
    // 获取input
    handleInfoInput(e) {
      console.log('InfoInput值： ' + e.detail.value)
      this.setData({
        upInfo: e.detail.value
      })
    },
    
    handleCaloricInput(e) {
      console.log('CaloricInput值： ' + e.detail.value)
      this.setData({
        upCaloric: e.detail.value
      })
    },
    handleCaloric(){
      if(this.data.inv == 0){
        this.setData({
          upCaloric: parseInt(this.data.upCaloric*(-100))
        })
      }
      if(this.data.inv == 1){
        this.setData({
          upCaloric: parseInt(this.data.upCaloric*(100))
        })
      }
      
    },
    // 更新信息
    addGoods() {
      if (!this.data.goodsInfo) {
        this.setData({
          goodsInfo: {} // 初始化 goodsInfo 对象为空对象
        });
      }
      const goodsInfo = this.data.goodsInfo;
      const {
        upuid,
        upName,
        upInfo,
        upSpecification,
        upCaloric,
      } = this.data;
      console.log(upuid + '!!!!!!!');
      goodsInfo.uid = upuid;
      goodsInfo.info = upName;
      if (upName !== '') {
        goodsInfo.name = upName;
      }
      if (upInfo !== '') {
        goodsInfo.info = upInfo;
      }
      if (upSpecification !== '') {
        goodsInfo.specification = upSpecification;
      }
      if (upCaloric !== '') {
        var regex = /^\d+(\.\d{1,2})?$/;
        if(!regex.test(upCaloric)){
          wx.showToast({
            title: '金额输入格式有误',
            icon: 'none',
            duration: 500
        });
          return;
        }
        this.handleCaloric();
        goodsInfo.caloric = this.data.upCaloric; // Assuming caloric value is a number
      }
      if(upCaloric == ''){
        wx.showToast({
          title: '金额不能为空',
          icon: 'none',
          duration: 500
      });
      return; // 提前退出函数
      }
      if(this.data.timeStamp === ''){
        const nowDate = new Date();
        this.data.timeStamp = nowDate.getTime();
      }
      goodsInfo.kind = this.data.inv;
      goodsInfo.uid = getApp().globalData.uid;
      goodsInfo.pid = 0;
      goodsInfo.delFlag = 0;
      goodsInfo.timeStamp = this.data.timeStamp;
      console.log(goodsInfo)
      wx.request({
        url: `${url}/sys-detail/add?caloricSum=${goodsInfo.caloric}
        &date=${goodsInfo.timeStamp}
        &info=${goodsInfo.info}
        &kind=${goodsInfo.kind}
        &name=${goodsInfo.name}
        &pid=${goodsInfo.pid}
        &quantity=0
        &specification=${goodsInfo.specification}
        &uid=${goodsInfo.uid}`,
        method: 'POST',
        success: function (res) {
          console.log('数据发送成功', res);
          console.log('数据发送成功', goodsInfo.uid);
          console.log(res.statusCode);
          if (res.statusCode == 200) {
            wx.showToast({
              title: '记账成功',
              icon: 'success',
              duration: 500 //持续的时间
            })
            setTimeout(function () {
              wx.switchTab({
                url: '../goodsListNew/goodsList'
              });
            }, 500)
          } 
          else {
            wx.showToast({
              title: '记账失败',
              icon: 'error',
              duration: 500 //持续的时间
            })
            // 关闭当前页面，跳转页面。
            setTimeout(function () {
              wx.switchTab({
                url: '../goodsListNew/goodsList'
              });
            }, 500)
          }
        },
        fail: function (err) {
          console.error('数据发送失败', err);
          wx.showToast({
            title: '失败!',
            icon: 'error',
            duration: 2000 //持续的时间
          })
        },
      });
    },

    categoryClick(e) {
      let categoryItem = e.currentTarget.dataset.item;
      this.setData({
          currentCategorySelect: categoryItem.iconStyleName,
          currentCategoryColor: categoryItem.color,
          currentCategoryName: categoryItem.name,
          currentCategorySelect1: categoryItem.iconStyleName,
          currentCategoryColor1: categoryItem.color,
          currentCategoryName1: categoryItem.name,
          upName:categoryItem.name,
          upSpecification:categoryItem.iconStyleName,
      });
  },
  categoryClick2(e) {
      let categoryItem = e.currentTarget.dataset.item;
      this.setData({
          currentCategorySelect: categoryItem.iconStyleName,
          currentCategoryColor: categoryItem.color,
          currentCategoryName: categoryItem.name,
          currentCategorySelect2: categoryItem.iconStyleName,
          currentCategoryColor2: categoryItem.color,
          currentCategoryName2: categoryItem.name,
          upName:categoryItem.name,
          upSpecification:categoryItem.iconStyleName,
      });
  },
  // 时间选择器
  showPicker(e) {
    const {
      mode
    } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const {
      mode
    } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const {
      value
    } = e.detail;
    const {
      mode
    } = this.data;
    // 获取用户选择的年、月、日
    const selectedTime = new Date(value);
    const year = selectedTime.getFullYear();
    const month = selectedTime.getMonth();
    const day = selectedTime.getDate();
    // 获取当前时间的时、分、秒
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();
    // 构建新的日期对象，日期部分为用户选择的年、月、日，时、分、秒部分为当前时间的时、分、秒
    const timestampDate = new Date(year, month, day, hours, minutes, seconds);
    // 获取该新日期对象的时间戳
    const timestamp = timestampDate.getTime(); // 获取选中时间的时间戳
    console.log('confirm', value);
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
      timeStamp: timestamp, // 存储时间戳
    });
    this.hidePicker();
  },
  onColumnChange(e) {
    console.log('pick', e.detail.value);
  },
  },
});